//
//  ViewController.m
//  王宇先_13_周末xib可视化豆瓣数据
//
//  Created by wyx on 16/3/26.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "TableViewOfBeef.h"
#import "CellOfFilm.h"
#import "JSONBeef.h"
#import "ModelOfBeef.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain)TableViewOfBeef *tableView;
@property (nonatomic, retain)NSMutableArray *arr;
@end

@implementation ViewController
#pragma mark - delloac
-(void)dealloc{
    [_tableView release];
    [_arr release];
    [super dealloc];
}
#pragma mark - 加载
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"豆瓣";
    [self createTableView];
    [self handleOfData];
    
}
#pragma mark - Data
- (void)handleOfData{
    id result = [JSONBeef JSONBeefWithFileName:@"activitylist" type:@"txt"];
    NSDictionary *dicOfBeef = [result objectForKey:@"events"];
    self.arr = [NSMutableArray array];
    //遍历数组
    for (NSDictionary *dic in dicOfBeef) {
        ModelOfBeef *model = [[ModelOfBeef alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.arr addObject:model];
        [model release];
        
    }
}
#pragma mark - 创建 TableView
- (void)createTableView{
    self.tableView = [[TableViewOfBeef alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    //指定代理人
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorColor = [UIColor redColor];
    
    //注册xib 格式的 Cell
    UINib *nib = [UINib nibWithNibName:@"CellOfFilm" bundle:[NSBundle mainBundle]];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"pool"];
}
#pragma mark - 协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellOfFilm *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    //通过 API赋值
    ModelOfBeef *model = [self.arr objectAtIndex:indexPath.row];
    [cell setCellContentValue:model];
    cell.selectionStyle = UITableViewCellAccessoryNone;
    return cell;
}
#pragma mark - delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 250;
}
#pragma mark - other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
